package com.huawei.hwid.api.common;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import com.huawei.cloudservice.CloudAccount;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.cloudservice.CloudRequestHandler;
import com.huawei.cloudservice.IntentResultHandler;
import com.huawei.cloudservice.LogoutHandler;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hwid.api.common.apkimpl.a;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.datatype.UserAccountInfo;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import java.util.ArrayList;
import java.util.Iterator;

@SuppressLint({"InlinedApi"})
public class f {
    public static void a(Context context, String str, CloudRequestHandler cloudRequestHandler, Bundle bundle) {
        if (!o.a(context, cloudRequestHandler)) {
            e.b("CloudAccountImpl2", "checkHwIdPassword: context or cloudRequestHandler is null", true);
        } else if (TextUtils.isEmpty(str)) {
            cloudRequestHandler.onError(new ErrorStatus(12, "the param is invalid"));
            e.b("CloudAccountImpl2", "userId is empty", true);
        } else if (!b.h(context)) {
            e.b("CloudAccountImpl2", "checkIsUseHuaweiAccount false", true);
            cloudRequestHandler.onError(new ErrorStatus(33, "hwid is not exit"));
        } else if (o.b(context)) {
            CloudAccount b = e.b(context, str);
            if (b == null) {
                cloudRequestHandler.onError(new ErrorStatus(12, "the param is invalid"));
                e.b("CloudAccountImpl2", "userId is error", true);
                return;
            }
            a(context, str, cloudRequestHandler, bundle, b);
        } else {
            e.b("CloudAccountImpl2", "hwid is not install", true);
            cloudRequestHandler.onError(new ErrorStatus(34, "hwid is not exit"));
        }
    }

    private static void a(final Context context, final String str, final CloudRequestHandler cloudRequestHandler, final Bundle bundle, CloudAccount cloudAccount) {
        cloudAccount.getUserInfo(context, "0001", new CloudRequestHandler() {
            public void onFinish(Bundle bundle) {
                e.a("CloudAccountImpl2", "getVerifiedPhone getUserInfo", true);
                ArrayList parcelableArrayList = bundle.getParcelableArrayList("userAccountInfo");
                String str = "";
                str = "";
                boolean z = bundle.getBoolean(HwAccountConstants.EXTRA_ONLY_QUERY, false);
                if (parcelableArrayList != null && parcelableArrayList.size() > 0) {
                    Bundle bundle2 = new Bundle();
                    CharSequence a = f.c(parcelableArrayList);
                    CharSequence b = f.d(parcelableArrayList);
                    if (!TextUtils.isEmpty(a)) {
                        bundle2.putString("secrityPhoneOrsecrityEmail", a);
                    }
                    if (!TextUtils.isEmpty(b)) {
                        bundle2.putString("accountName", b);
                    }
                    if (!TextUtils.isEmpty(a) || !TextUtils.isEmpty(b)) {
                        bundle2.putString(HiAnalyticsConstant.BI_KEY_RESUST, "1");
                        cloudRequestHandler.onFinish(bundle2);
                    } else if (z) {
                        e.b("CloudAccountImpl2", "phone is not exist", true);
                        cloudRequestHandler.onError(new ErrorStatus(46, "phone number is not exist"));
                    } else {
                        f.c(context, str, cloudRequestHandler, bundle);
                    }
                } else if (z) {
                    e.b("CloudAccountImpl2", "phone or email is not exist", true);
                    cloudRequestHandler.onError(new ErrorStatus(46, "phone number is not exist"));
                } else {
                    f.c(context, str, cloudRequestHandler, bundle);
                }
            }

            public void onError(ErrorStatus errorStatus) {
                cloudRequestHandler.onError(errorStatus);
            }
        });
    }

    private static String c(ArrayList<UserAccountInfo> arrayList) {
        String str = "";
        if (arrayList != null && arrayList.size() > 0) {
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                String userAccount;
                UserAccountInfo userAccountInfo = (UserAccountInfo) it.next();
                if (HwAccountConstants.TYPE_SECURITY_PHONE.equals(userAccountInfo.getAccountType()) && "1".equals(userAccountInfo.getAccountState())) {
                    userAccount = userAccountInfo.getUserAccount();
                    e.a("CloudAccountImpl2", "VerifiedPhone ", true);
                } else {
                    userAccount = str;
                }
                str = userAccount;
            }
        }
        return str;
    }

    private static String d(ArrayList<UserAccountInfo> arrayList) {
        String str = "";
        if (arrayList != null && arrayList.size() > 0) {
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                String userAccount;
                UserAccountInfo userAccountInfo = (UserAccountInfo) it.next();
                if ("2".equals(userAccountInfo.getAccountType())) {
                    userAccount = userAccountInfo.getUserAccount();
                    e.a("CloudAccountImpl2", "phoneAccountName ", true);
                } else {
                    userAccount = str;
                }
                str = userAccount;
            }
        }
        return str;
    }

    private static void c(Context context, String str, CloudRequestHandler cloudRequestHandler, Bundle bundle) {
        if (b.d(context, str) == null) {
            cloudRequestHandler.onError(new ErrorStatus(13, "no account by userId"));
            return;
        }
        bundle.putString("bindOperation", "1");
        e.a(context, cloudRequestHandler);
        e.a(context, str, false, new CloudRequestHandler() {
            public void onFinish(Bundle bundle) {
                e.b("CloudAccountImpl2", "onFinish for Pay, do nothing", true);
            }

            public void onError(ErrorStatus errorStatus) {
                e.b("CloudAccountImpl2", "onFinish for Pay, do nothing", true);
            }
        }, bundle);
    }

    public static void a(Context context, String str, String str2, Bundle bundle, LogoutHandler logoutHandler) throws RuntimeException {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            e.b("CloudAccountImpl2", "hwid is not exit", true);
            throw new IllegalArgumentException("this interface can not be called in main thread");
        } else if (logoutHandler == null) {
            e.b("CloudAccountImpl2", "handler is null", true);
        } else if (context == null) {
            e.b("CloudAccountImpl2", "getAccountsByType: context is null", true);
            logoutHandler.onFail(new ErrorStatus(12, "context is null"));
        } else if (!b.h(context)) {
            e.b("CloudAccountImpl2", "can not use hwid", true);
            logoutHandler.onFail(new ErrorStatus(33, "can not use hwid"));
        } else if (!o.b(context)) {
            e.b("CloudAccountImpl2", "hwid is not exit", true);
            logoutHandler.onFail(new ErrorStatus(34, "hwid is not exit"));
        } else if (o.d(context) <= 20503000) {
            e.d("CloudAccountImpl2", "hwid verson not support logout interface", true);
            logoutHandler.onFail(new ErrorStatus(35, "hwid apk version not support logout interface"));
        } else {
            a a = a.a(context);
            if (a == null) {
                logoutHandler.onFail(new ErrorStatus(40, "null == manager"));
                e.d("CloudAccountImpl2", "manager is null", true);
                return;
            }
            a.a(new m(context, str, str2, bundle, logoutHandler));
        }
    }

    public static long a(Context context) {
        return a.b(context);
    }

    public static void a(Context context, int i, int i2, boolean z, IntentResultHandler intentResultHandler) {
        if (intentResultHandler == null) {
            e.b("CloudAccountImpl2", "handler is null", true);
        } else if (context == null) {
            e.b("CloudAccountImpl2", "context is null", true);
            intentResultHandler.onError(new ErrorStatus(12, "context is null"));
        } else if (!o.b(context)) {
            e.b("CloudAccountImpl2", "hwid is not exit", true);
            intentResultHandler.onError(new ErrorStatus(34, "hwid is not exit"));
        } else if (o.a(context, 20601300)) {
            e.b("CloudAccountImpl2", "hwid version is low", true);
            intentResultHandler.onError(new ErrorStatus(35, "hwid is low version"));
        } else if (b.a(context)) {
            Bundle bundle = new Bundle();
            bundle.putInt(CloudAccountManager.KEY_LOGIN_CHANNEL, i);
            bundle.putInt(CloudAccountManager.KEY_REQCLIENTTYPE, i2);
            bundle.putBoolean("isOOBE", z);
            a.a(context, HwAccountConstants.FORGOT_PWD_INTENT, "", bundle, intentResultHandler);
        } else {
            e.b("CloudAccountImpl2", "no network", true);
            intentResultHandler.onError(new ErrorStatus(5, "no network"));
        }
    }
}
